package com.javaclimb.music.contrller;


import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.SongList;
import com.javaclimb.music.service.SongListService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 歌手控制类
 */

@RequestMapping(value = "/songList",method = RequestMethod.POST)/*或者@PostMapping()*/
@RestController
public class SongListController {

    @Autowired
    private SongListService SongListService;

    /**
     * 添加歌手
     */
    @RequestMapping(value ="/add",method = RequestMethod.POST)
    public Object addSongList(HttpServletRequest request){
        JSONObject jsonObject=new JSONObject();
        String title=request.getParameter("title").trim();//姓名
        String style=request.getParameter("style").trim();//性别
        String pic=request.getParameter("pic").trim();//头像
        String introduction=request.getParameter("introduction").trim();//简介


        //保存歌手到对象中去
        SongList SongList=new SongList();
        SongList.setTitle(title);
        SongList.setPic(pic);
        SongList.setStyle(style);
        SongList.setIntroduction(introduction);
        boolean flag=SongListService.insert(SongList);
        if(flag){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加成功");

            return  jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"添加失败");

        return  jsonObject;
    }

    /**
     * 修改歌手
     */
    @RequestMapping(value ="/update",method = RequestMethod.POST)
    public Object updateSongList(HttpServletRequest request){
        JSONObject jsonObject=new JSONObject();
        String id=request.getParameter("id").trim();
        String title=request.getParameter("title").trim();//姓名
        String style=request.getParameter("style").trim();//性别
        String introduction=request.getParameter("introduction").trim();//简介
        //保存歌手到对象中去
        SongList SongList=new SongList();
        SongList.setId(Integer.parseInt(id));
        SongList.setTitle(title);
        SongList.setStyle(style);
        SongList.setIntroduction(introduction);
        boolean flag=SongListService.update(SongList);
        if(flag){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");

            return  jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"修改失败");

        return  jsonObject;
    }

    /**
     * 删除歌手
     */
    @RequestMapping(value ="/delete",method = RequestMethod.GET)
    public Object deleteSongList(HttpServletRequest request){
        String id=request.getParameter("id").trim();//主键id
        boolean flag=SongListService.delete(Integer.parseInt(id));
        return  flag;
    }
    /**
     * 根据主键查询整个对象
     */
    @RequestMapping(value ="/selectByPrimaryKey",method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id=request.getParameter("id").trim();
        return SongListService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的歌手
     */
    @RequestMapping(value ="/allSongList",method = RequestMethod.GET)
    public Object allSongList(HttpServletRequest request) {

        return SongListService.allSongList();
    }
    /**
     * 根据歌手名字模糊查询列表
     */
    @RequestMapping(value ="/songListOfTitle",method = RequestMethod.GET)
    public Object SongListOfName(HttpServletRequest request) {
     String title=request.getParameter("title").trim();
        return SongListService.SongListOfTitle(title);
    }
    /**
     * 根据歌手名字查询列表
     */
    @RequestMapping(value ="/likeTitle",method = RequestMethod.GET)
    public Object likeTitle(HttpServletRequest request) {
        String title=request.getParameter("title").trim();
        return SongListService.LikeTitle("%"+title+"%");
    }



/***
 * 更新歌手图片
 *
 */

    @PostMapping("updateSigerPic")
    public  Object updateSigerPic(@RequestParam("file")MultipartFile mf,@RequestParam("id") int id) throws IOException {


        JSONObject jsonObject=new JSONObject();
        if(mf.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改图片失败");
            return jsonObject;
        }

        String fileName=System.currentTimeMillis()+mf.getOriginalFilename();
        String filePath=System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"songList"+System.getProperty("file.separator");

        File file1=new File(filePath);
        if(!file1.exists()){
            file1.mkdirs();
        }

        //实际文件地址
        File f2=new File(filePath+System.getProperty("file.separator")+fileName);

        //存储到数据库中的图片路径

        String storeAvatorPath="img/songList/"+fileName;

        //上传过程
        mf.transferTo(f2);

        SongList SongList=new SongList();

        SongList.setId(id);
        SongList.setPic(storeAvatorPath);

        boolean flag=SongListService.update(SongList);

        if(flag){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改图片成功");
            jsonObject.put("pic",storeAvatorPath);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"修改图片失败");
        return jsonObject;
    }




}
